package cn.itsource.basic.basic.controller;

import cn.itsource.basic.basic.domain.dto.EmailDto;
import cn.itsource.basic.basic.domain.dto.SendSmsDto;
import cn.itsource.basic.basic.service.IVerifyCodeService;
import cn.itsource.basic.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    private IVerifyCodeService verifyCodeService;

    @GetMapping("/image/{imageCodeKey}")
    public AjaxResult verifyCodeImage(@PathVariable("imageCodeKey") String key){
        try {
            String s = verifyCodeService.image(key);
            return AjaxResult.me().setResultObj(s);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("服务器忙，请稍后重试！"+e.getMessage());
        }
    }

    @PostMapping("/sendSmsCode")
    public AjaxResult sendSmsCode(@RequestBody SendSmsDto sendSmsDto){
        try {
            String s = verifyCodeService.sendSmsCode(sendSmsDto);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }

    @PostMapping("/sendEmail")
    public AjaxResult sendEmail(@RequestBody EmailDto emailDto){
        try {
            System.out.println(111111111);
            String s = verifyCodeService.sendEmail(emailDto);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }
}
